/* 
剔除对象中某个值 e代表对象 k表示要剔除的属性名
*/

const filterObject = (e, k) => {
    let form = {}
    //判断单个是字符串
    if (typeof (k) == 'string') {
        for (let key in e) {
            if (key != k) {
                form[key] = e[key]
            }
        }
    }
    //判断数组的方式
    else if (Array.isArray(k)) {
        for (let key in e) {
            if (!k.includes(key)) {
                form[key] = e[key]
            }
        }
    }
    return form
}

/* 
@剔除不查询的字段
query 是req arr是需要回显字段集合
*/
const filterQuery = (query) => {
    return new Promise((resolve, reject) => {
        let obj = {}
        let arrs = ['pageNo']
        for (let key in query) {
            if (!arrs.includes(key) && query[key] != null && query[key] != '' && query[key] != undefined) {
                obj[key] = query[key]
            }
        }
        resolve(Object.keys(obj))
    })
}


/* 
@sql 字段循环拼接 sqls 总数
*/
const pinjieSql = (arr, sql, sqls) => {
    return new Promise((resolve, reject) => {
        arr.forEach((el, index) => {
            console.log(el, 'el')
            if (index != arr.length - 1) {
                sql += ` ${el} = ? and `
                sqls += ` ${el} = ? and `
            }
            else {
                sql += ` ${el} = ? ORDER BY date DESC limit ?,?`
                sqls += ` ${el} = ?`
            }
        })
        let arrs = [sql, sqls]
        resolve(arrs)
    })
}


/* 
@ sql总数拼接like
*/
const pinjieLike = (arr, sql, sqls, req) => {
    return new Promise((resolve, reject) => {
        console.log(arr, 'arr')
        arr.forEach((el, ind) => {
            if (ind != arr.length - 1) {
                sql += ` ${el} like ` + ` '%${req.query[el]}%' ` + ' and '
                sqls += ` ${el} like ` + ` '%${req.query[el]}%' ` + ' and '
            }
            else {
                sql += ` ${el} like ` + ` '%${req.query[el]}%' ` + ' ORDER BY date DESC limit ?,? '
                sqls += ` ${el} like ` + ` '%${req.query[el]}%' `
            }
        })
        let arrs = [sql, sqls]
        resolve(arrs)
    })
}



/* 
@
mysql查询字段集合
*/
const filterArrs = (arr, query) => {
    return new Promise((resolve, reject) => {
        let arrs = []
        arr.forEach((els, index) => {
            arrs.push(query[els])
        })
        resolve(arrs)
    })
}


/* 
@ 联表拼接  nameList表名 id 属性关联
*/
const pinjieMore = (nameList) => {
    return new Promise((resolve, reject) => {
        let sql = `select * from ${nameList[0]} inner join `
        nameList.forEach((el, ind) => {
            if (ind > 0) {
                if (ind != nameList.length - 1) {
                    sql += `${el} on ${nameList[0]}.id = ${el}.id join `
                }
                else {
                    sql += `${el} on ${nameList[0]}.id = ${el}.id where ${nameList[0]}.id = ?`
                }

            }
        })
        console.log(sql, 'sql')
        resolve(sql)
    })
}


/* 
@ 编辑联表(单条,用id)
*/
const pinjieEdit = (sql, nameList) => {
    return new Promise((resolve, reject) => {
        console.log(sql, 'sql')
        nameList.forEach((el, ind) => {
            if (ind == 0) {
                sql += `set ${el} = ? , `
            }
            else if (ind != nameList.length - 1) {
                sql += `${el} = ? , `
            }
            else {
                sql += `${el} = ? where id = ? `
            }

        })
        resolve(sql)
    })
}


/* 
@ 递归式
*/

const recursion = (start, arr) => {
    let arrs = []
    arr.forEach(ele => {
        if (ele.pid == start) {
            ele.children = []
            ele.children = [...recursion(ele.id, arr)]
            arrs.push(ele)
        }
    })
    return arrs
}


/* 
@   模糊查询 arrs上边可自定义
*/

const pinjiemohu = (sql, arrs, req) => {
    return new Promise((resolve, reject) => {
        arrs.forEach((el, ind) => {
            if (ind != arrs.length - 1) {
                sql += `${el} like '%${req.query['keyword']}%' or `
            }
            else {
                sql += `${el} like '%${req.query['keyword']}%'`
            }
        })
        console.log(sql, 'sql')
        resolve(sql)
    })
}


/* 
@ 批量删除
*/

const deleteMore = (arr, sql) => {
    return new Promise((resolve, reject) => {
        arr.forEach((el, ind) => {
            if (arr.length == 1) {
            sql+=` (?) `
            }
            else {
                if (ind == 0) {
                    sql += ` (?,`
                }
                else if (ind != arr.length - 1) {
                    sql += ` ?,`
                }
                else {
                    sql += `?)`
                }
            }
        })
        resolve(sql)
    })
}


/* 
@自定义id长度
*/
const createIds = (arr) => {
    let id = ""
    arr.forEach(ele => {
        let index = Math.floor(Math.random() * 10)
        id += index
    })
    return id
}


module.exports = {
    filterObject,
    filterQuery,
    pinjieSql,
    filterArrs,
    pinjieMore,
    pinjieLike,
    pinjieEdit,
    recursion,
    pinjiemohu,
    deleteMore,
    createIds
}